home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_gnu / adainc / s-wchstw.adb < prev    next >
Text File  |  1996-01-30  |  4KB  |  108 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT RUNTIME COMPONENTS                          --
  4. --                                                                          --
  5. --                       S Y S T E M . W C H _ S T W                        --
  6. --                                                                          --
  7. --                                 B o d y                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.5 $                              --
  10. --                                                                          --
  11. --           Copyright (c) 1992,1993,1994 NYU, All Rights Reserved          --
  12. --                                                                          --
  13. -- The GNAT library is free software; you can redistribute it and/or modify --
  14. -- it under terms of the GNU Library General Public License as published by --
  15. -- the Free Software  Foundation; either version 2, or (at your option) any --
  16. -- later version.  The GNAT library is distributed in the hope that it will --
  17. -- be useful, but WITHOUT ANY WARRANTY;  without even  the implied warranty --
  18. -- of MERCHANTABILITY  or  FITNESS FOR  A PARTICULAR PURPOSE.  See the  GNU --
  19. -- Library  General  Public  License for  more  details.  You  should  have --
  20. -- received  a copy of the GNU  Library  General Public License  along with --
  21. -- the GNAT library;  see the file  COPYING.LIB.  If not, write to the Free --
  22. -- Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.        --
  23. --                                                                          --
  24. ------------------------------------------------------------------------------
  25.  
  26. with System.WCh_Con; use System.WCh_Con;
  27. with System.WCh_JIS; use System.WCh_JIS;
  28.  
  29. package body System.WCh_StW is
  30.  
  31.    ---------------------------
  32.    -- String_To_Wide_String --
  33.    ---------------------------
  34.  
  35.    function String_To_Wide_String
  36.      (S    : String;
  37.       EM   : WC_Encoding_Method)
  38.       return Wide_String
  39.    is
  40.       R  : Wide_String (1 .. S'Length);
  41.       RP : Natural;
  42.       SP : Natural;
  43.       C1 : Natural;
  44.       C2 : Natural;
  45.  
  46.       function Get_Hex (C : Character) return Natural;
  47.       --  Converts character from hex digit to value in range 0-15. The
  48.       --  input must be in 0-9, A-F (upper case), and no check is needed.
  49.  
  50.       function Get_Hex (C : Character) return Natural is
  51.       begin
  52.          if C in '0' .. '9' then
  53.             return Character'Pos (C) - Character'Pos ('0');
  54.          else
  55.             return Character'Pos (C) - Character'Pos ('A') + 10;
  56.          end if;
  57.       end Get_Hex;
  58.  
  59.    --  Start of processing for Wide_Image
  60.  
  61.    begin
  62.       SP := S'First;
  63.       RP := 0;
  64.  
  65.       while SP <= S'Last loop
  66.          RP := RP + 1;
  67.  
  68.          if (S (SP) = Ascii.ESC and then EM = WCEM_Hex) then
  69.             R (RP) := Wide_Character'Val (
  70.                Get_Hex (S (SP + 4)) + 16 *
  71.                  (Get_Hex (S (SP + 3)) + 16 *
  72.                    (Get_Hex (S (SP + 2)) + 16 *
  73.                      (Get_Hex (S (SP + 1))))));
  74.             SP := SP + 5;
  75.  
  76.          --  One-byte ASCII character
  77.  
  78.          elsif S (SP) <= Ascii.DEL or else EM = WCEM_Hex then
  79.             R (RP) := Wide_Character'Val (Character'Pos (S (SP)));
  80.             SP := SP + 1;
  81.  
  82.             --  Upper bit shift, internal code = external code
  83.  
  84.          elsif EM = WCEM_Upper then
  85.             R (RP) := Wide_Character'Val (
  86.                         Character'Pos (S (SP)) * 256 +
  87.                         Character'Pos (S (SP + 1)));
  88.             SP := SP + 2;
  89.  
  90.          --  Upper bit shift, EUC
  91.  
  92.          elsif EM = WCEM_EUC then
  93.             R (RP) := EUC_To_JIS (S (SP), S (SP + 1));
  94.             SP := SP + 2;
  95.  
  96.          --  Upper bit shift, shift-JIS
  97.  
  98.          else -- EM = WCEM_Shift_JIS
  99.             R (RP) := Shift_JIS_To_JIS (S (SP), S (SP + 1));
  100.             SP := SP + 2;
  101.          end if;
  102.       end loop;
  103.  
  104.       return R (1 .. RP);
  105.    end String_To_Wide_String;
  106.  
  107. end System.WCh_StW;
  108.